package com.shinechain.TulingBot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class Tuling {
    private static String APIKEY = "1f271f08fefe4c2baaf5c01e000882a9";

    public static String chat(String question) throws IOException {
        String resp = "";
        //String question = "大连今天天气怎样";// 这是上传给云机器人的问题
        String INFO = URLEncoder.encode(question, "utf-8");
        String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY
                + "&info=" + INFO;
        URL getUrl = new URL(getURL);
        HttpURLConnection connection = (HttpURLConnection) getUrl
                .openConnection();
        connection.connect();

        // 取得输入流，并使用Reader读取
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(connection.getInputStream(), "utf-8"));
        StringBuffer sb = new StringBuffer();
        String line = "";
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        reader.close();
        // 断开连接
        connection.disconnect();
        resp = sb.toString();
        String answer;
        String []ss = new String[10];
        ss = resp.split(":");
        answer = ss[ss.length-1];
        answer = answer.substring(1,answer.length()-2);
        System.out.println(sb);

        return answer;
    }
}
